package csc.fresher.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.AccountState;
import csc.fresher.entity.Customer;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.service.SavingAccountService;

@Component
@Scope("request")
public class SavingAccountManagedBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	SavingAccountService savingAccountService;

	private final static String[] states;

	static {
		states = new String[4];
		states[0] = AccountState.ACTIVE.getState();
		states[1] = AccountState.DISABLE.getState();
		states[2] = AccountState.NEW.getState();
		states[3] = AccountState.REMOVABLE.getState();
	}

	private FacesContext context = FacesContext.getCurrentInstance();

	private List<CustomerAccount> cusAccounts;

	private String message;

	private Customer customer;

	private String accountNumber;

	private String idCardNumber;

	private List<CustomerAccount> filteredCusAccounts;

	public List<CustomerAccount> getFilteredCusAccounts() {
		return filteredCusAccounts;
	}

	public void setFilteredCusAccounts(List<CustomerAccount> filteredCusAccounts) {
		this.filteredCusAccounts = filteredCusAccounts;
	}

	public String getAccountNumber() {
		return accountNumber;
	}

	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}

	public String getIdCardNumber() {
		return idCardNumber;
	}

	public void setIdCardNumber(String idCardNumber) {
		this.idCardNumber = idCardNumber;
	}

	private SelectItem[] statesOption;

	public SelectItem[] getStatesOption() {
		return statesOption;
	}

	public void setStatesOption(SelectItem[] statesOption) {
		this.statesOption = statesOption;
	}

	private SelectItem[] createFilterOptions(String[] data) {
		SelectItem[] options = new SelectItem[data.length + 1];

		options[0] = new SelectItem("", "Select");
		for (int i = 0; i < data.length; i++) {
			options[i + 1] = new SelectItem(data[i], data[i]);
		}

		return options;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<CustomerAccount> getCusAccounts() {
		return cusAccounts;
	}

	public void setCusAccounts(List<CustomerAccount> cusAccounts) {
		this.cusAccounts = cusAccounts;
	}

	@PostConstruct
	public void init() {
		if (savingAccountService != null) {
			cusAccounts = savingAccountService.getAllSavingAccounts();
			System.out.println(cusAccounts.size());
		} else {
			System.out.println("saving Account Service is null");
		}
	}

	public void refresh() {
		redirect("saving-acc-list.jsf");
	}

	public void search() {
		if (savingAccountService != null) {
			cusAccounts = new ArrayList<>();
			customer = null;
			message = "";
			if (idCardNumber.equals("") && accountNumber.equals("")) {
				message = "Please enter account number or ID card number you want to search.";
			} else {
				if (!idCardNumber.equals("")) {
					long id = Long.parseLong(idCardNumber);
					cusAccounts = savingAccountService
							.getListCustomerAccountByIDCardNumber(id);
					if (cusAccounts != null && cusAccounts.size() > 0) {
						CustomerAccount customerAccount = cusAccounts.get(0);
						customer = savingAccountService
								.getCustomerByCustomerID(customerAccount
										.getCustomerID());
					}
				}
				if ((cusAccounts == null || cusAccounts.size() == 0)
						&& !accountNumber.equals("")) {
					long accNumber = Long.parseLong(accountNumber);
					CustomerAccount customerAccount = savingAccountService
							.getCustomerAccountByID(accNumber);
					if (customerAccount != null) {
						cusAccounts.add(customerAccount);
						customer = savingAccountService
								.getCustomerByCustomerID(customerAccount
										.getCustomerID());
					}
				}
				if (cusAccounts == null || cusAccounts.size() == 0) {
					message = "Not found any account";
				}
			}
			
		} else {
			message = "Error";
		}
		if (message != null && !message.equals("")) {
			//context.addMessage(null, new FacesMessage("Message", message));
		}
	}

	private void redirect(String url) {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		try {
			externalContext.redirect(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
